package com.jiazhong.dao.impl;

import com.jiazhong.dao.DBUtil;
import com.jiazhong.dao.UserDao;
import com.jiazhong.model.User;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class UserDaoImpl extends DBUtil implements UserDao {
    @Override
    public void addUser(User user) {
        String sql="insert into tbl_user values(default,?,?)";
        super.executeUpdate(sql,user.getUser_username(),user.getUser_password());
    }

    @Override
    public void updateUser(User user) {
        String sql="update tbl_user set user_name=?,user_password=? where user_id=?";
        super.executeUpdate(sql,user.getUser_username(),user.getUser_password(),user.getUser_id());
    }

    @Override
    public void delUserById(int userId) {
        String sql="delete from tbl_user where user_id=?";
        super.executeUpdate(sql,userId);
    }

    @Override
    public List<User> queryUsers() {
        try {
            super.getConn();
            String sql="select * from tbl_user";
            ps = conn.prepareStatement(sql);
            rs= ps.executeQuery();
            List<User> users=new ArrayList<>();
            while (rs.next()){
                int userId = rs.getInt(1);
                String username = rs.getString(2);
                String password = rs.getString(3);
                User user=new User();
                user.setUser_id(userId);
                user.setUser_username(username);
                user.setUser_password(password);
                users.add(user);
            }
            return users;
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            super.closeAll();
        }
        return null;
    }

    @Override
    public User queryUserById(int userId) {
        try {
            super.getConn();
            String sql="select * from tbl_user where user_id=?";
            ps=conn.prepareStatement(sql);
            ps.setInt(1,userId);
            rs = ps.executeQuery();
            while (rs.next()){
                User user=new User();
                user.setUser_id(rs.getInt(1));
                user.setUser_username(rs.getString(2));
                user.setUser_password(rs.getString(3));
                return user;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            super.closeAll();
        }
        return null;
    }

    @Override
    public User queryUserByUsernamePassword(String username, String password) {
        try {
            super.getConn();
            String sql="select * from tbl_user where user_name=? and user_password=?";
            ps=conn.prepareStatement(sql);
            ps.setString(1,username);
            ps.setString(2,password);
            rs = ps.executeQuery();
            while (rs.next()){
                User user=new User();
                user.setUser_id(rs.getInt(1));
                user.setUser_username(rs.getString(2));
                user.setUser_password(rs.getString(3));
                return user;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            super.closeAll();
        }
        return null;
    }
}
